package com.yuanfeng.payment.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.*;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.payment.dto.PayCardRecordDTO;
import com.yuanfeng.payment.entity.PayCardEntity;
import com.yuanfeng.payment.entity.PayCardRecordEntity;
import com.yuanfeng.payment.mapper.PayCardMapper;
import com.yuanfeng.payment.mapper.PayCardRecordMapper;
import com.yuanfeng.payment.service.PayCardRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName PayCardRecordServiceImpl
 * @Description 生成充值卡信息记录表
 * @Author cyp
 * @Date 2023-05-31 15:35:50
 * @Version 1.0
 **/
@Service
public class PayCardRecordServiceImpl extends ServiceImpl<PayCardRecordMapper, PayCardRecordEntity>
        implements PayCardRecordService {
    @Autowired
    private PayCardMapper payCardMapper;

    @Override
    public ResponseResult query(PayCardRecordDTO payCardRecordDto) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(payCardRecordDto.getToken());
        if (userInfo == null) {
            userInfo = ParseTokenUtils.getPlatformUser(payCardRecordDto.getToken());
        }
        if (null != userInfo) {
            try {
                //将对象转换为map
                Map data = ParseMapUtils.beanToMap(payCardRecordDto);
                //分页查询数据
                IPage<PayCardRecordEntity> page = this.baseMapper.query(new Query<PayCardRecordEntity>().getPage(data), payCardRecordDto);

                return ResponseResult.success(PageUtils.getPage(page));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }

        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);
    }

    @Override
    public ResponseResult add(PayCardRecordDTO param) {
        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(param.getToken());
        if (null != userInfo) {
            // 创建时间
            Date creatTime = DateUtils.getNowDate();
            List<PayCardEntity> pay = new ArrayList<PayCardEntity>();

            for (int y = 1; y <= param.getCardNumber(); y++) {
                String str = new DecimalFormat("0000").format(y);
                // 随机密码
                String password = StringRandomUtil.getStringRandom(8);
                // 随机卡号
                String cardNum = StringRandomUtil.getStringRandom(4) + str + StringRandomUtil.getStringNum();

                PayCardEntity num = new PayCardEntity();
                num.setCardDesc(param.getCardDesc());
                num.setCardNum(cardNum);
                num.setCreatTime(creatTime);
                num.setEtime(param.getEtime());
                num.setPassword(password);
                num.setStatut(CommonType.other.NOT_USED.getCode());
                num.setStime(param.getStime());
                num.setTotalPrice(param.getTotalPrice());
                num.setUseName(param.getUseName());
                pay.add(num);
            }
            try {
                payCardMapper.addPayCardtew(pay);
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED.getCode(), BizCodeEnume.OPERATION_FAILED.getMsg());
            }

            PayCardRecordEntity payCardRecord = new PayCardRecordEntity();
            payCardRecord.setCardCreator(param.getUseName());
            payCardRecord.setTotalPrice(param.getTotalPrice());
            payCardRecord.setCardDesc(param.getCardDesc());
            payCardRecord.setCreatTime(creatTime);
            payCardRecord.setCardNumber(param.getCardNumber());
            payCardRecord.setCardStart(pay.get(0).getId());
            payCardRecord.setCardEnd(pay.get(0).getId() + param.getCardNumber() - 1);
            try {
                this.baseMapper.insert(payCardRecord);
            } catch (Exception e) {
                e.printStackTrace();
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ResponseResult.fail(BizCodeEnume.OPERATION_FAILED.getCode(), BizCodeEnume.OPERATION_FAILED.getMsg());
            }

            return ResponseResult.success();
        }
        return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL);

    }

}
